package com.seek.servicebase.exceptionhandler;

import com.seek.commonutils.ExceptionUtil;
import com.seek.commonutils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * author wml
 * Created on 2022/1/10 17:54
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 全局异常
    @ExceptionHandler(Exception.class) // 指定出现什么异常执行此方法
    @ResponseBody
    public ResponseResult error(Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseResult.error().message("执行了全局异常处理...");
    }

    // 特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResponseResult error(ArithmeticException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseResult.error().message("执行了 ArithmeticException 异常处理...");
    }

    // 自定义异常
    @ExceptionHandler(SeekException.class)
    @ResponseBody
    public ResponseResult error(SeekException e) {
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return ResponseResult.error().code(e.getCode()).message(e.getMsg());
    }
}
